<?php

/**
 * +------ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER * ---------+
 * +----------------------------------------------------------------------------+
 * | PHPKiisf - Keep It in Simple Framework										|
 * | Version 1.0																|
 * | Copyright (C) <2013>  Mario Cezzare <mcezzare@gmail.com>					|
 * | Blog: http://mcezzare.blogspot.com.br										|
 * |																			|
 * | This program is part of PHPKiisf, and is free software: you can			|
 * | redistribute it and/or modify it under the terms of the GNU 				|
 * | General Public License as published by										|
 * | the Free Software Foundation, either version 3 of the License, or			|
 * | (at your option) any later version.										|
 * |																			|
 * | This program is distributed in the hope that it will be useful,			|
 * | but WITHOUT ANY WARRANTY; without even the implied warranty of				|
 * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the				|
 * | GNU General Public License for more details.								|
 * | You should have received a copy of the GNU General Public License			|
 * | along with this program.  If not, see <http://www.gnu.org/licenses/>.		|
 * +----------------------------------------------------------------------------+
 * | Author : <Mario Cezzare Angelicola Chiodi> <mcezzare@gmail.com>			|
 * +----------------------------------------------------------------------------+
 * | Contributors																|
 * |																			|
 * +----------------------------------------------------------------------------+
 *
 */
/**
 * @package	Dao
 * @access	public
 * @since	2013-03-08
 * @author	Mario Cezzare <mcezzare@gmail.com>
 */

/**
 * DESCRIPTION
 * This is an example of DAO
 *
 *
 */
class default_dao {

	/**
	 * Connection handler
	 *
	 * @var connection
	 */
	protected $connection ;

	/**
	 * Array of tables of current DB.
	 * Paranoid security
	 *
	 * @var array
	 */
	protected static $session_tables ;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->connection = new connection() ;
		$this->set_session_tables( $this->get_tables() ) ;
	}

	/**
	 * Called by autoloader
	 *
	 * @return void
	 */
	public static function init() {
//				if ( $this->connection->status == 0 ) {
//						$this->connection = new connection();
//				}

		$message = $this->connection->error ;
		if ( $message != null ) {
			utils::inform_user( $message ) ;
		}
	}

	/**
	 * Get tables from current mysql connection
	 *
	 * @param boolean $return_text if create numeric indexes
	 * @return array
	 */
	public function get_tables( $return_text = false ) {
		$sql = "SHOW TABLES" ;
		// $sql	 = sprintf( "SELECT * FROM information_schema.tables WHERE table_schema = '%s' " , DB_NAME );
		$r = $this->connection->execute_query( $sql ) ;
		$tables = null ;

		if ( $r ) {
			/* For select boxes , for example */
			if ( $return_text ) {
				while ( $row = mysql_fetch_array( $r ) ) {
					// $tables[ $row{"TABLE_NAME"} ] = $row{"TABLE_NAME"};
					$tables[$row{0}] = $row{0} ;
				}
				return $tables ;
			}

			/* does not need create else code */
			while ( $row = mysql_fetch_array( $r ) ) {
				// $tables[ $row{"TABLE_NAME"} ] = $row{"TABLE_NAME"};
				$tables[$row{0}] = $row{0} ;
			}
		}
		$this->session_tables = $tables ;
		return $tables ;
	}

	/**
	 * Get Database table columns
	 *
	 * @param type $table_name Table name
	 *
	 * @return Array Array of columns
	 */
	public function get_columns_from_table( $table_name ) {
		$info = null ;
		$table_columns = null ;

		/**
		 * paranoid block
		 */
		if ( !array_key_exists( $table_name , $this->session_tables ) ) {
			display_user_message( sprintf( "%s does not exists." , $table_name ) ) ;
			/**
			 * return null
			 */
			return $info ;
		}
		if ( $table_name !== null ) {
			$sql = sprintf( "SHOW FULL COLUMNS  from %s" , $table_name ) ;
			$info = $this->get_full_description( $sql , $table_name ) ;
		}
		if ( is_array( $info ) ) {
			foreach ( $info as $key => $field ) {
				$table_columns[] = array(
					"index" => $key ,
					"table_name" => $table_name ,
					"field" => $field[0] ,
					"type" => $field[1] ,
					"collation" => $field[2] ,
					"null" => $field[3] ,
					"key" => $field[4] ,
					"default" => $field[5] ,
					"extra" => $field[6] ,
					"privileges" => $field[7] ,
					"comment" => $field[8] ,
						) ;
			}
			$info = $table_columns ;
		}

		return $info ;
	}

	/**
	 * Get Database table properties
	 *
	 * @param type $table_name Table name
	 *
	 * @return Array Array of properties
	 *
	 * TABLE_CATALOG,TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE,ENGINE,VERSION,
	 * ROW_FORMAT,TABLE_ROWS,AVG_ROW_LENGTH,DATA_LENGTH,MAX_DATA_LENGTH,
	 * INDEX_LENGTH,DATA_FREE,AUTO_INCREMENT,CREATE_TIME,UPDATE_TIME,
	 * CHECK_TIME,TABLE_COLLATION,CHECKSUM,CREATE_OPTIONS,TABLE_COMMENT
	 */
	public function get_table_properties( $table_name ) {
		$info = null ;
		if ( $table_name !== null ) {
			$sql = sprintf( "SELECT * FROM information_schema.tables WHERE table_schema = '%s' AND TABLE_NAME = '$table_name' " , DB_NAME ) ;
			$info = $this->get_full_description( $sql , $table_name ) ;
		}
		if ( is_array( $info ) ) {
			foreach ( $info as $key => $field ) {
				$table_properties[] = array(
					"INDEX" => $key ,
					"TABLE_CATALOG" => $field[0] ,
					"TABLE_SCHEMA" => $field[1] ,
					"TABLE_NAME" => $field[2] ,
					"TABLE_TYPE" => $field[3] ,
					"ENGINE" => $field[4] ,
					"VERSION" => $field[5] ,
					"ROW_FORMAT" => $field[6] ,
					"TABLE_ROWS" => $field[7] ,
					"AVG_ROW_LENGTH" => $field[8] ,
					"DATA_LENGTH" => $field[9] ,
					"MAX_DATA_LENGTH" => $field[10] ,
					"INDEX_LENGTH" => $field[11] ,
					"DATA_FREE" => $field[12] ,
					"AUTO_INCREMENT" => $field[13] ,
					"CREATE_TIME" => $field[14] ,
					"UPDATE_TIME" => $field[15] ,
					"CHECK_TIME" => $field[16] ,
					"TABLE_COLLATION" => $field[17] ,
					"CHECKSUM" => $field[18] ,
					"CREATE_OPTIONS" => $field[19] ,
					"TABLE_COMMENT" => $field[20] ,
						) ;
			}
			$info = $table_properties ;
		}

		return $info ;
	}

	/**
	 *
	 * @param type $table_name
	 * @return type
	 *
	 * table,non_unique,key_name,seq_in_index,column_name,
	 * collation,cardinality,sub_part,packed,null,index_type,comment
	 */
	public function get_table_indexes( $table_name ) {
		$info = null ;
		if ( $table_name !== null ) {
			$sql = sprintf( "SHOW INDEX FROM %s.%s" , DB_NAME , $table_name ) ;
			$info = $this->get_full_description( $sql , $table_name ) ;
		}

		if ( is_array( $info ) ) {
			foreach ( $info as $key => $field ) {
				$table_properties[] = array(
					"INDEX" => $key ,
					"table" => $field[0] ,
					"non_unique" => $field[1] ,
					"key_name" => $field[2] ,
					"seq_in_index" => $field[3] ,
					"column_name" => $field[4] ,
					"collation" => $field[5] ,
					"cardinality" => $field[6] ,
					"sub_part" => $field[7] ,
					"packed" => $field[8] ,
					"null" => $field[9] ,
					"index_type" => $field[10] ,
					"comment" => $field[11] ,
						) ;
			}
			$info = $table_properties ;
		}

		return $info ;
	}

	/**
	 *
	 *
	 * @param type $sql
	 * @param type $object
	 * @return type
	 */
	private function get_full_description( $sql , $object ) {

		$info = null ;
		$sql = sprintf( $sql , $object ) ;
		$rs = $this->connection->execute_query( $sql ) ;
		while ( $row = mysql_fetch_row( $rs ) ) {
			$info[] = $row ;
		}
		return $info ;
	}

	public function get_connection() {
		return $this->connection ;
	}

	public function set_connection( connection $connection ) {
		$this->connection = $connection ;
	}

	public function get_session_tables() {
		return $this->session_tables ;
	}

	public function set_session_tables( $session_tables ) {
		$this->session_tables = $session_tables ;
	}

}

?>